package com.iot.inclinometer.ui.oldproject;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.iot.inclinometer.R;
import com.iot.inclinometer.common.BaseFragment;
import com.iot.inclinometer.project.entity.BaseInfo;
import com.iot.inclinometer.project.service.Impl.ProjectServiceImpl;
import com.iot.inclinometer.project.service.ProjectService;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

@ContentView(R.layout.fragment_old)
public class OldProjectFragment extends BaseFragment {

    @ViewInject(R.id.project_name_spinner)
    Spinner project_name_spinner;
    @ViewInject(R.id.item_name_spinner)
    Spinner item_name_spinner;
    @ViewInject(R.id.drill_num_spinner)
    Spinner drill_num_spinner;
    @ViewInject(R.id.deep_of_hole_view)
    TextView deep_of_hole_view;
    @ViewInject(R.id.mark1_view)
    TextView mark1_view;
    @ViewInject(R.id.mark2_view)
    TextView mark2_view;
    @ViewInject(R.id.mark3_view)
    TextView mark3_view;
    @ViewInject(R.id.mark4_view)
    TextView mark4_view;
    @ViewInject(R.id.loc_view)
    TextView loc_view;
    @ViewInject(R.id.azimuth_view)
    TextView azimuth_view;
    @ViewInject(R.id.x_view)
    TextView x_view;
    @ViewInject(R.id.y_view)
    TextView y_view;
    @ViewInject(R.id.delete_pro_btn)
    Button delete_pro_btn;

    private ProjectService projectService = new ProjectServiceImpl();
    private ArrayAdapter<String> projectAdapter;
    private ArrayAdapter<String> itemAdapter;
    private ArrayAdapter<String> holeAdapter;
    private String projectName;
    private String itemName;
    private String drillNum;

    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        initProjectMenu();
        project_name_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                initItemMenu();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });

        item_name_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                initHoleNumMenu();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });

        drill_num_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                initValue();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });
    }

    @Event(value = R.id.delete_pro_btn)
    private void deleteProBtnOnClick(View v){
        boolean success = projectService.delete(project_name_spinner.getSelectedItem().toString(),item_name_spinner.getSelectedItem().toString(),drill_num_spinner.getSelectedItem().toString());
        if(success){
            Toast.makeText(getContext(),"删除成功",Toast.LENGTH_SHORT).show();
            initProjectMenu();
        }else{
            Toast.makeText(getContext(),"删除失败",Toast.LENGTH_SHORT).show();
        }
    }

    private void initProjectMenu(){
        List<String> nameList = projectService.queryProjectName();

// 把数组导入到ArrayList中
        projectAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_item,nameList);
        projectAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 设置下拉菜单的风格
        project_name_spinner.setAdapter(projectAdapter);
        // 绑定适配器
        project_name_spinner.setPrompt("请选择项目");
        if(nameList.isEmpty()){
            initItemMenu();
        }
    }

    private void initItemMenu(){
        if(project_name_spinner.getSelectedItem()==null){
            item_name_spinner.setAdapter(null);
            projectName = null;
            initHoleNumMenu();
            return;
        }
        projectName = project_name_spinner.getSelectedItem().toString();
        List<String> nameList = projectService.queryItemNameByProjectName(projectName);

        itemAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_item,nameList);
        itemAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 设置下拉菜单的风格
        item_name_spinner.setAdapter(itemAdapter);
        // 绑定适配器
        item_name_spinner.setPrompt("请选择工点");
    }

    private void initHoleNumMenu(){
        if(project_name_spinner.getSelectedItem()==null){
            drill_num_spinner.setAdapter(null);
            projectName = null;
            itemName = null;
            drillNum = null;
            initValue();
            return;
        }
        projectName = project_name_spinner.getSelectedItem().toString();
        itemName = item_name_spinner.getSelectedItem().toString();
        List<String> nameList = projectService.queryDrillNumByItemName(projectName,itemName);
        holeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_item,nameList);
        holeAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 设置下拉菜单的风格
        drill_num_spinner.setAdapter(holeAdapter);
        // 绑定适配器
        drill_num_spinner.setPrompt("请选择孔位");
    }

    private void initValue(){
        if(projectName!=null&&itemName!=null){
        System.out.println(projectName);
            BaseInfo queryEntity = new BaseInfo();
            drillNum = drill_num_spinner.getSelectedItem().toString();
            queryEntity.setProject_name(projectName).setItem_name(itemName).setDrill_num(drillNum);
            BaseInfo baseInfo = projectService.queryOne(queryEntity);
            deep_of_hole_view.setText(baseInfo.getDepth());
            mark1_view.setText(baseInfo.getMark1());
            mark2_view.setText(baseInfo.getMark2());
            mark3_view.setText(baseInfo.getMark3());
            mark4_view.setText(baseInfo.getMark4());
            loc_view.setText(baseInfo.getLoc());
            azimuth_view.setText(baseInfo.getAzimuth());
            x_view.setText(baseInfo.getX());
            y_view.setText(baseInfo.getY());
            delete_pro_btn.setEnabled(true);
        }else{
            deep_of_hole_view.setText("0");
            mark1_view.setText("null");
            mark2_view.setText("0");
            mark3_view.setText("null");
            mark4_view.setText("0");
            loc_view.setText("无");
            azimuth_view.setText("0");
            x_view.setText("0");
            y_view.setText("0");
            delete_pro_btn.setEnabled(false);
        }
    }
}